package handler

import (
	"fmt"
	
	"os"
	"path/filepath"
	"strconv"
	"strings"
	
	"github.com/henrylee2cn/faygo"
)

var Index2 = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	return ctx.Render(200,"view2/index.html",nil)
})

var Home2 = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	fmt.Println("返回登录页,销毁Seesion")
	ctx.DestroySession()
	return ctx.Render(200, "view2/login.html", faygo.Map{})
})

type Stafflogin struct {
	Username string `param:"<in:formData><required><length:5:11>"`
	Password string `param:"<in:formData><required><length:5:20>"`
}

func (tmpObj *Stafflogin) Serve(ctx *faygo.Context) error {
	fmt.Println("接收对象", tmpObj)
	var staff Staff
	staff.Username = tmpObj.Username
	staff.Password = tmpObj.Password
	if exist, err := Engine.Get(&staff); exist && err == nil {
		fmt.Println("存在员工", exist, err)
		ctx.SetSession("username", staff.Username)
		ctx.SetSession("uid", int(staff.Id))
		fmt.Println("设置UID", staff.Id)
		return ctx.Redirect(301, "/index2")
	} else {
		fmt.Println("不存在员工", exist, err)
		return Home2(ctx)
	}
}

var Request = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	return ctx.Render(200, "view2/email.html", faygo.Map{
		"list": func() (list []Email) {
			Engine.Desc("create_time").Find(&list, &Email{Sid: ctx.GetSession("uid").(int)})
			return list
		},
		"name": ctx.GetSession("username").(string),
		"uid":  ctx.GetSession("uid").(int),
	})
})

var Newlist = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	return ctx.Render(200, "view2/newlist.html", faygo.Map{
		"list": func() (list []News) { Engine.Desc("create_time").Where("status != 2").Find(&list); return list },
		"name": ctx.GetSession("username").(string),
		"uid":  ctx.GetSession("uid").(int),
	})
})

var Newlist2 = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	return ctx.Render(200, "view2/newlist2.html", faygo.Map{
		"list": func() (list []News) { Engine.Desc("create_time").Where("status != 2").Find(&list); return list },
		"name": ctx.GetSession("username").(string),
		"uid":  ctx.GetSession("uid").(int),
	})
})

var Getnews = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	tmpId := ctx.Param("id")
	id, _ := strconv.Atoi(tmpId)
	var news News
	exist, err := Engine.ID(id).Get(&news)
	fmt.Print("查找新闻", id, exist, err)
	return ctx.Render(200, "view2/newsdetail.html", faygo.Map{
		"obj": news,
	})
})

var StaffList2 = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	return ctx.Render(200, "view2/staff.html", faygo.Map{
		"list1": func() (list []Staff) { Engine.Desc("create_time").Find(&list, &Staff{Type: 2}); return list },
		"list2": func() (list []Staff) { Engine.Desc("create_time").Find(&list, &Staff{Type: 1, Own: 1}); return list },
		"list3": func() (list []Staff) { Engine.Desc("create_time").Find(&list, &Staff{Type: 1, Own: 2}); return list },
		"list4": func() (list []Staff) { Engine.Desc("create_time").Find(&list, &Staff{Type: 1, Own: 3}); return list },
		"name":  ctx.GetSession("username").(string),
		"uid":   ctx.GetSession("uid").(int),
	})
})

var Myreply = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	return ctx.Render(200, "view2/reply.html", faygo.Map{
		"list": func() (list []Reply) {
			Engine.Desc("create_time").Find(&list, &Reply{Sid: ctx.GetSession("uid").(int)})
			fmt.Println("留言", len(list))
			return list
		},
		"name": ctx.GetSession("username").(string),
		"uid":  ctx.GetSession("uid").(int),
	})
})

func (tmpObj *Reply) Serve(ctx *faygo.Context) error {
	option := ctx.Param("option")
	switch option {
	case "CCC":
		fmt.Println("新建数据", tmpObj)
		row, err := Engine.Omit("create_time", "id", "read").InsertOne(tmpObj)
		fmt.Println("CCC", row, err, tmpObj)
		return ctx.JSON(200, faygo.Map{
			"row": row,
			"err": err,
		})
	case "UUU":
		fmt.Println("更新数据", tmpObj)
		row, err := Engine.ID(tmpObj.Id).Omit("create_time").Update(tmpObj)
		fmt.Println("UUU", row, err, tmpObj)
		return ctx.JSON(200, faygo.Map{
			"row": row,
			"err": err,
		})
	case "RRR":
		fmt.Println("查询数据", tmpObj)
		var obj = Reply{Id: tmpObj.Id}
		exist, err := Engine.Get(&obj)
		fmt.Println("RRR", exist, err, tmpObj)
		return ctx.JSON(200, faygo.Map{
			"obj": obj,
			"err": err,
		})
	case "DDD":
		fmt.Println("删除数据", tmpObj)
		row, err := Engine.ID(tmpObj.Id).Delete(Reply{Id: tmpObj.Id})
		fmt.Println("DDD", row, err, tmpObj)
		return ctx.JSON(200, faygo.Map{
			"row": row,
			"err": err,
		})
	default:
		return ctx.JSON(200, faygo.Map{"err": "CURD操作失败!"})
	}
}

var Filelist = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	var list []file
	filepath.Walk("./upload", func(path string, f os.FileInfo, err error) error {
		if !f.IsDir() {
			var tmp = file{strings.Replace(path, "upload/", "", 1), f.ModTime().Format("2006-01-02 15:04"), path}
			list = append(list, tmp)
		}
		return nil
	})
	return ctx.Render(200, "view2/filelist.html", faygo.Map{
		"list": list,
		"name": ctx.GetSession("username").(string),
		"uid":  ctx.GetSession("uid").(int),
	})
})

type file struct {
	Name        string
	Create_time string
	Url         string
}

var Rencai2 = faygo.HandlerFunc(func(ctx *faygo.Context) error {
	return ctx.Render(200, "view2/rencai.html", faygo.Map{
		"list": func() (list []Rc) {
			Engine.Find(&list)
			return list
		},
		"name": ctx.GetSession("username").(string),
		"uid":  ctx.GetSession("uid").(int),
	})
})